package sdp.group14.adapter;

import java.util.ArrayList;

import sdp.group14.R;
import sdp.group14.appdata.Event;
import sdp.group14.appdata.Request;
import sdp.group14.views.StaffEventListItem;
import sdp.group14.views.StaffRequestListItem;
import sdp.group14.views.TenantEventListItem;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * this class is used to store all the Request in a list
 * @author ste706
 *
 */
public class TenantEventsListAdapter extends BaseAdapter {
	
	private ArrayList<Event> events;
	private Context context;

	public TenantEventsListAdapter(Context context, ArrayList<Event> events) {
		this.events = events;
		this.context = context;
	}

	/**
	 * get the size of the Requests array
	 */
	public int getCount() {
		return events.size();
	}

	/**
	 * get the Requests id
	 */
	public long getEventId(Event e)
	{
		return e.getEventID();
	}
	

	/**
	 * return the Request at the given position
	 */
	public Event getItem(int position) {
		return (null == events) ? null : events.get(position);
	}

	/**
	 * return the position with long type
	 */
	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * delete the Request in the given position of the Requests array 
	 * @param position
	 */
	public void deleteItem(int position){
		events.remove(position);
	}
	
	/**
	 * delete the given Request in the  Requests array 
	 * @param position
	 */
	
	/*
	public void deleteRequest(Event event){
		try{
		events.remove(event);
		notifyDataSetChanged();
		}
		catch(Exception e)
		{
			Log.e("requestAdapter", "errors occured in request deleting");
		}
	}*/
	/**
	 * register the adapter with the given xml layout to display the
	 * list
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		
		try
		{
		TenantEventListItem tli;
		if (null == convertView) {
			tli = (TenantEventListItem)View.inflate(context, R.layout.tenant_event_list_item, null);
		} else {
			tli = (TenantEventListItem)convertView;
		}
		tli.setEvent(events.get(position));
		return tli;
		}
		catch(Exception e)
		{
			Log.e("TenantEventsListAdapter", e.getMessage());
		}
		return null;
	}

	public void forceReload() {
		notifyDataSetChanged();
	}

	public void removeAllRequests() {
		events.clear();
		notifyDataSetChanged();
	}

}
